import { ref } from 'vue';
import axios from 'axios';

// API基地址
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || '';

// 表数组
const tableNames = ['users', 'articles', 'products', 'movies', 'comments', 'categories'];

// 获取类型索引
const getTypeIndex = () => {
  return 2
};

// 通用API服务
const apiService = {
  // 用户注册
register: async (userData) => {
  try {
    const response = await axios.post(`${API_BASE_URL}/api/demo/reg`, userData);
    return response.data;
  } catch (error) {
    console.error('Registration failed:', error);
    throw error;
  }
},
  
  // 用户登录
  login: async (userData) => {
    try {
      const response = await axios.post(`${API_BASE_URL}/api/demo/login`, userData);
      return response.data;
    } catch (error) {
      console.error('Login failed:', error);
      throw error;
    }
  },
  
  // 获取列表数据
  getAllItems: async (params) => {
    try {
      const response = await axios.get(`${API_BASE_URL}/api/demo/getallitems`, {
        params: { 
          typeIndex: getTypeIndex()
        }
      });
      return response.data;
    } catch (error) {
      console.error('Failed to fetch items:', error);
      throw error;
    }
  },
  
  
  // 获取单条数据
  getSingle: async (params) => {
    try {
      const response = await axios.get(`${API_BASE_URL}/api/demo/getsingle`, {
        params: { id: params.id, typeIndex: getTypeIndex() }
      });
      return response.data;
    } catch (error) {
      console.error('Failed to fetch item:', error);
      throw error;
    }
  },
  
  // 更新数据
  updateItem: async (itemData) => {
    try {
      const response = await axios.put(`${API_BASE_URL}/api/demo/update`, itemData);
      return response.data;
    } catch (error) {
      console.error('Failed to update item:', error);
      throw error;
    }
  },
  
  // 删除数据
  deleteItem: async (params) => {
    try {
      const response = await axios.delete(`${API_BASE_URL}/api/demo/delete`, {
        params: { id: params.id, typeIndex: params.typeIndex }
      });
      return response.data;
    } catch (error) {
      console.error('Failed to delete item:', error);
      throw error;
    }
  },
  
  // 获取电影列表
 // src/services/api.js
getMovies: async (params = {}) => {
  try {
    const response = await axios.get(`${API_BASE_URL}/api/demo/getallitems`, {
      params: { typeIndex: getTypeIndex(), ...params }
    });
    return response.data;
  } catch (error) {
    console.error('Failed to fetch movies:', error);
    throw error;
  }
},
  // 添加电影
  addMovie: async (movieData) => {
    return this.addItem({...movieData, typeIndex: getTypeIndex()});
  },
  
  // 获取单个电影
  getMovie: async (id) => {
    return this.getSingle({ id, typeIndex: getTypeIndex() });
  },
  
  // 更新电影
  updateMovie: async (id, movieData) => {
    return this.updateItem({id, ...movieData, typeIndex: getTypeIndex()});
  },
  
  // 删除电影
  deleteMovie: async (id) => {
    return this.deleteItem({id, typeIndex: getTypeIndex()});
  },
  
  // 获取评论
getComments: async (params) => {
  try {
    const response = await axios.get(`${API_BASE_URL}/api/demo/getallitems`, {
      params: { 
        typeIndex: 4,
        articleid: params.articleid,
        replyType: '商品'
      }
    });
    return response.data;
  } catch (error) {
    console.error('Failed to fetch comments:', error);
    throw error;
  }
},
  
  // 添加评论
addComment: async (commentsData) => {
  try {
    const response = await axios.post(`${API_BASE_URL}/api/demo/additem`, {
      articleid: commentsData.articleid,
      body: commentsData.body,
      userid: commentsData.userid,
      typeIndex: 4,
      replyType: '商品'
    });
    return response.data;
  } catch (error) {
    console.error('Failed to add comment:', error);
    throw error;
  }
},
  // 添加数据
  addItem: async (itemData) => {
    try {
      const response = await axios.post(`${API_BASE_URL}/api/demo/additem`, {
        desc:itemData.desc,//描述
        title:itemData.title,
        picPaths:itemData.picPaths,  //海报链接
        price:itemData.price,
        typeIndex:getTypeIndex(),
        userId:itemData.userId,

      });
      return response.data;
    } catch (error) {
      console.error('Failed to add item:', error.message);
      throw error;
    }
  }
};

export default apiService;